﻿using BlondeHeaven.BLL.Interface;
using BlondeHeaven.DB.Model;
using BlondeHeaven.DTO;
using BlondeHeaven.DTO.Interface;
using BlondeHeaven.UI.Models.Shop;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace BlondeHeaven.UI.Controllers
{
    public class ShopController : Controller
    {
        private IBllShopKeeperService<ShopKeeper> _db;

        public ShopController(IBllShopKeeperService<ShopKeeper> db)
        {
            _db = db;
        }
        public IActionResult Index()
        {
            var shop1 = _db.GetAllshop();
            List<RegisterShopViewModel> registerVOM = new List<RegisterShopViewModel>();
            foreach (var item in shop1)
            {
                var registerShopViewModel = new RegisterShopViewModel();
                VmDateBase(item, registerShopViewModel);
                registerVOM.Add(registerShopViewModel);
            }
            return View(registerVOM);
        }
        /// <summary>
        /// 根据模糊名字查询
        /// </summary>
        /// <param name="SearchString"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Index(string SearchString)
        {
            if (SearchString == null)
            {
                return RedirectToAction("index");
            }
            var shop1 = _db.GetByoneName(SearchString);
            List<RegisterShopViewModel> registerVOM = new List<RegisterShopViewModel>();
            foreach (var item in shop1)
            {
                var registerShopViewModel = new RegisterShopViewModel();
                VmDateBase(item, registerShopViewModel);
                registerVOM.Add(registerShopViewModel);
            }
            return View(registerVOM);
        }



        [HttpGet]
        public IActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Register(RegisterShopViewModel ShopViewModel)
        {
            ShopKeeper shop = new ShopKeeper();
            ModelDataBase(ShopViewModel, shop);
            shop.UserId = 2;
            _db.Add(shop, true);
            return RedirectToAction("index");
        }

        [HttpGet]
        public IActionResult Edit(int Id)
        {
            var shop = _db.GetByoneId(Id);
            RegisterShopViewModel ShopViewModel = new RegisterShopViewModel();
            VmDateBase(shop, ShopViewModel);
            return View(ShopViewModel);
        }
        [HttpPost]
        public IActionResult Edit(RegisterShopViewModel ShopViewModel)
        {
            ShopKeeper shop = new ShopKeeper();
            ModelDataBase(ShopViewModel, shop);
            _db.ChangeShopKeeperInformation(shop, true);
            return RedirectToAction("index");
        }
        [HttpGet]
        public IActionResult Delete(int id, int userid, RegisterShopViewModel registerShopViewModel)
        {

            bool Isremo = true;
            var shop = _db.GetByoneId(registerShopViewModel.Id = id);
            VmDateBase(shop, registerShopViewModel, Isremo);
            return View(registerShopViewModel);
        }
        [HttpPost]
        public IActionResult Delete(RegisterShopViewModel registerShopViewModel)
        {

            ShopKeeper shop = new ShopKeeper()
            {
                Id = registerShopViewModel.Id,
            };
            _db.RemoveAsync(shop, true);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Details(int id, RegisterShopViewModel registerShopViewModel)
        {
            var shop = _db.GetByoneId(registerShopViewModel.Id = id);
            VmDateBase(shop, registerShopViewModel);
            return View(registerShopViewModel);
        }
        /// <summary>
        /// 天花板数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="ShopViewModel"></param>
        private static void VmDateBase(ShopKeeper shopKeeper, RegisterShopViewModel ShopVIewModel, bool IsRemo = false)
        {
            ShopVIewModel.UserId = shopKeeper.UserId;
            ShopVIewModel.Id = shopKeeper.Id;
            ShopVIewModel.Address = shopKeeper.Address;
            ShopVIewModel.Name = shopKeeper.Name;
            ShopVIewModel.Phone = shopKeeper.Phone;
            ShopVIewModel.Photo = shopKeeper.Photo;
            ShopVIewModel.Sales = shopKeeper.Sales;
            if (IsRemo)
            {
                ShopVIewModel.IsRemo = true;
            }
            else
            {
                ShopVIewModel.IsRemo = false;
            }
        }
        private static void ModelDataBase(RegisterShopViewModel ShopViewModel, ShopKeeper shop)
        {
            shop.Id = ShopViewModel.Id;
            shop.UserId = ShopViewModel.UserId;
            shop.Name = ShopViewModel.Name;
            shop.Address = ShopViewModel.Address;
            shop.Sales = ShopViewModel.Sales;
            shop.Phone = ShopViewModel.Phone;
            shop.Photo = ShopViewModel.Photo;
        }





    }
}
